(function ($) {

    var nav_click, content_wrapper, logoutBtn, updatePwdModalValidate;

    var indexPage = {};

    indexPage.init = function () {

        App.setbasePath(base_url);
        App.setGlobalImgPath(base_url + "/adminlte/dist/img/");

        initTab();

        App.fixIframeCotent();
        initMenu();
        initUserMenu();


    }


    function initMenu() {
        var menus = [
            {
                id: "9000",
                text: "header",
                icon: "",
                isHeader: true
            },
            {
                id: "9001",
                text: "系统管理",
                icon: "fa fa-laptop",
                children: [
                    {
                        id: "90012",
                        text: "用户管理",
                        url: base_url +"/pages/user/userList",
                        targetType: "iframe-tab",
                        icon: "fa fa-circle-o"
                    }
                ]
            },
            {
                id: "9002",
                text: "短链接链接",
                icon: "fa fa-edit",
                url: base_url +"/pages/slink/slinkList",
                targetType: "iframe-tab",
            }
        ];
        $('.sidebar-menu').sidebarMenu({data: menus});

        // 动态创建菜单后，可以重新计算 SlimScroll
        // $.AdminLTE.layout.fixSidebar();

        /*if ($.AdminLTE.options.sidebarSlimScroll) {
            if (typeof $.fn.slimScroll != 'undefined') {
                //Destroy if it exists
                var $sidebar = $(".sidebar");
                $sidebar.slimScroll({destroy: true}).height("auto");
                //Add slimscroll
                $sidebar.slimscroll({
                    height: ($(window).height() - $(".main-header").height()) + "px",
                    color: "rgba(0,0,0,0.2)",
                    size: "3px"
                });
            }
        }*/
    }

    function initTab() {
        addTabs({
            id: '10008',
            title: '欢迎页',
            close: false,
            url: base_url + '/pages/welcome',
            urlType: "relative"
        });
    }

    function initUserMenu() {
        logoutBtn = $("#logoutBtn");
        // logout
        logoutBtn.click(function () {
            layer.confirm("确定注销登录", {
                icon: 3,
                title: "系统提示",
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);

                $.post(base_url + "/user/logout", function (data, status) {
                    if (data.code == "200") {
                        layer.msg("注销成功");
                        setTimeout(function () {
                            window.location.href = base_url + "/";
                        }, 500);
                    } else {
                        layer.open({
                            title: "系统提示",
                            btn: ["确定"],
                            content: (data.msg || "注销失败"),
                            icon: '2'
                        });
                    }
                });
            });

        });


        // update pwd
        $('#updatePwd').on('click', function () {
            $('#updatePwdModal').modal({backdrop: false, keyboard: false}).modal('show');
        });


        $("#updatePwdModal").on('hide.bs.modal', function () {
            $("#updatePwdModal .form")[0].reset();
            updatePwdModalValidate.resetForm();
            $("#updatePwdModal .form .form-group").removeClass("has-error");
        });


        updatePwdModalValidate = $("#updatePwdModal .form").validate({
            errorElement: 'span',
            errorClass: 'help-block',
            focusInvalid: true,
            rules: {
                password: {
                    required: true,
                    rangelength: [4, 50]
                },
                rePassword: {
                    required: true,
                    rangelength: [4, 50]
                }
            },
            messages: {
                password: {
                    required: '请输入密码',
                    rangelength: "密码长度限制为4~50"
                },
                rePassword: {
                    required: '请重复输入密码',
                    rangelength: "密码长度限制为4~50"
                }
            },
            highlight: function (element) {
                $(element).closest('.form-group').addClass('has-error');
            },
            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },
            errorPlacement: function (error, element) {
                element.parent('div').append(error);
            },
            submitHandler: function (form) {
                taotaoLink.post0(base_url + "/user/updatePwd",
                    {
                        password: $('#updatePwdModal [name="password"]').val(),
                        rePassword: $('#updatePwdModal [name="rePassword"]').val()
                    }, function (data, status) {
                        $('#updatePwdModal').modal('hide');
                        layer.msg("修改密码成功，即将注销登陆");
                        setTimeout(function () {
                            $.post(base_url + "/user/logout", function (data, status) {
                                if (data.code == 200) {
                                    window.location.href = base_url + "/";
                                } else {
                                    layer.open({
                                        icon: '2',
                                        content: (data.msg || "注销失败")
                                    });
                                }
                            });
                        }, 500);

                    }, function (data, status) {

                        layer.open({
                            icon: '2',
                            content: (data.msg || "修改密码" + "失败")
                        });

                    }
                );
            }
        });

    }


    indexPage.start = function () {

        indexPage.init();
    }

    $(function () {
        indexPage.start();
    });
})(jQuery);